import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

import { PageParams } from './pagination.interface'

@Component({
    selector: 'pagination',
    templateUrl: './pagination.html',
    styleUrls: ['./pagination.css']
})

export class Pagination implements OnInit {
    /**
     * pagination
     * pageTotal,pageItems,pageCur
     */
    @Input() pagination: PageParams;
    @Input() maxSize: number = 7;
    @Output() pageChange: EventEmitter<any> = new EventEmitter;

    pageGoValue: number;
    pages: Array<any>;
    constructor() { }

    ngOnInit() {
        this.pagination.maxPage = Math.ceil(this.pagination.totalCount / this.pagination.pageItems);

        this.pagination.pageCur = this.pagination.pageCur ? this.pagination.pageCur : 1;
        this.pages = this.pageInit(this.pagination.pageCur);

    }


    nextPage() {
        if (this.isLastPage()) {
            return;
        }

        this.pagination.pageCur += 1;

        this.pageGo();
    }


    prePage() {
        if (this.isFirstPage()) {
            return;
        }

        this.pagination.pageCur -= 1;

        this.pageGo();
    }

    // 
    pageGo() {
        this.pages = this.pageInit(this.pagination.pageCur);
       
        // emit events
        this.pageChange.emit(this.pagination);
    }
    // 
    pageInput(pageValue) {
        pageValue = parseInt(pageValue);
        if (pageValue == this.pagination.pageCur || pageValue == '...' || pageValue > this.pagination.maxPage|| pageValue <1) {
            return;
        }
        

        this.pagination.pageCur = pageValue;        

        this.pageGo();
    }


    isFirstPage(): boolean {
        if (this.pagination.pageCur == 1) {
            return true;
        } else {
            return false;
        }
    }

    isLastPage(): boolean {
        if (this.pagination.pageCur == this.pagination.totalCount) {
            return true;
        } else {
            return false;
        }
    }


    // 分页页码算法  
    pageInit(curPage) {

        let pages = [];

        let range = this.maxSize;//去头去尾

        let maxPage = this.pagination.maxPage;

        // let curPage = this.pagination.pageCur;

        let isStart = curPage < range;

        let isEnd = curPage > maxPage - range;

        for (let i = 0; i < range; i++) {

            let pageNumber;
            if (isStart) {
                if (i + 1 == range) {
                    pageNumber = "...";
                } else {
                    pageNumber = i + 2;
                }
            } else if (isEnd) {
                if (i == 0) {
                    pageNumber = "...";
                } else {
                    pageNumber = maxPage - range + i;
                }
            } else {//middle
                if (i == 0) {
                    pageNumber = '...';
                } else if (i + 1 == range) {
                    pageNumber = '...';
                } else {
                    pageNumber = curPage + i + 1 - Math.round(range / 2);
                }
            }

            pages.push(pageNumber);

        }

        return pages;
    }


}